package com.ruoyi.web.controller.wxpay;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.mapper.OrderInfoMapper;
import com.ruoyi.web.pojo.OrderInfo;
import com.ruoyi.web.service.OrderInfoService;
import com.ruoyi.web.uniEnum.OrderInfoEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： OrderInfoController
 *  创建时间：2025/2/19
 */
@RestController
@RequestMapping("/order/info")
public class OrderInfoController {



    //订单
    @Resource
    private OrderInfoMapper orderInfoMapper;

    //订单
    @Resource
    private OrderInfoService orderInfoService;




    //积分账单
    @PostMapping("/get/order/records2")
    public AjaxResult getOrderRecords2(@RequestBody OrderInfo orderInfo){
        //创建分页对象
        Page<OrderInfo> page = new Page<>();
        //设置行数
        page.setSize(orderInfo.getSize());
        //设置页码
        page.setCurrent(orderInfo.getCurrent());
        //查询
        orderInfoMapper.selectPage(page,new LambdaQueryWrapper<OrderInfo>()
                //用户编号
                .eq(OrderInfo::getUserId, SecurityUtils.getUserId())
                //成功
                .in(OrderInfo::getOrderType, Arrays.asList(
                        OrderInfoEnum.FukuanChengGong.getType(),
                        OrderInfoEnum.HaoYouFaBuDongJie.getType(),OrderInfoEnum.HaoYouFaBuDongJieFanHuan.getType(),
                        OrderInfoEnum.ZhiDingXiaoFei.getType(),OrderInfoEnum.MingPianTuiKuan.getType(),OrderInfoEnum.MingPianKouKuan.getType(),
                        OrderInfoEnum.ZhiDingXiaoFei.getType(),OrderInfoEnum.GuangChangZhiDing.getType())
                )
                //逻辑删除
                .eq(OrderInfo::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(OrderInfo::getCreateTime)
                //分页
                .last(" limit " + (orderInfo.getCurrent() - 1) * orderInfo.getSize() +"," + orderInfo.getSize())
        );
        //设置总行数
        page.setTotal(orderInfoMapper.selectCount(new LambdaQueryWrapper<OrderInfo>()
                //用户编号
                .eq(OrderInfo::getUserId, SecurityUtils.getUserId())
                //成功
                .in(OrderInfo::getOrderType, Arrays.asList(
                        OrderInfoEnum.FukuanChengGong.getType(),
                        OrderInfoEnum.HaoYouFaBuDongJie.getType(),OrderInfoEnum.HaoYouFaBuDongJieFanHuan.getType(),
                        OrderInfoEnum.ZhiDingXiaoFei.getType(),OrderInfoEnum.MingPianTuiKuan.getType(),OrderInfoEnum.MingPianKouKuan.getType(),
                        OrderInfoEnum.ZhiDingXiaoFei.getType(),OrderInfoEnum.GuangChangZhiDing.getType())
                )
                //逻辑删除
                .eq(OrderInfo::getDeleteFlag,Boolean.FALSE)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //获取订单明细
    @PostMapping("/get/order/records")
    public AjaxResult getOrderRecords(@RequestBody OrderInfo orderInfo){
        //创建分页对象
        Page<OrderInfo> page = new Page<>();
        //设置行数
        page.setSize(orderInfo.getSize());
        //设置页码
        page.setCurrent(orderInfo.getCurrent());
        //查询
        orderInfoMapper.selectPage(page,new LambdaQueryWrapper<OrderInfo>()
                //用户编号
                .eq(OrderInfo::getUserId, SecurityUtils.getUserId())
                //成功
                .in(OrderInfo::getOrderType, Arrays.asList(OrderInfoEnum.TiXianWanCheng.getType(),
                        OrderInfoEnum.PingTaiYaoQingShouYi.getType(),
                        OrderInfoEnum.PingTaiRenWuShouYi.getType()))
                //逻辑删除
                .eq(OrderInfo::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(OrderInfo::getCreateTime)
                //分页
                .last(" limit " + (orderInfo.getCurrent() - 1) * orderInfo.getSize() +"," + orderInfo.getSize())
        );
        //设置总行数
        page.setTotal(orderInfoMapper.selectCount(new LambdaQueryWrapper<OrderInfo>()
                //用户编号
                .eq(OrderInfo::getUserId, SecurityUtils.getUserId())
                //成功
                .in(OrderInfo::getOrderType, Arrays.asList(OrderInfoEnum.TiXianWanCheng.getType(),
                        OrderInfoEnum.PingTaiYaoQingShouYi.getType(),
                        OrderInfoEnum.PingTaiRenWuShouYi.getType()))
                //逻辑删除
                .eq(OrderInfo::getDeleteFlag,Boolean.FALSE)
        ));
        //返回结果
        return AjaxResult.success(page);
    }



    //查询
    @PostMapping("/get/OrderInfo")
    public AjaxResult getOrderInfo(@RequestBody OrderInfo orderInfo){
        System.out.println();
        System.out.println(1212);
        // 当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = "";
        if(orderInfo.getCreateTime()!=null){
            formattedDate =  formatter.format(orderInfo.getCreateTime());
        }
        // 转换为字符串

        System.out.println(formattedDate);
        //创建分页对象
        Page<OrderInfo> page = new Page<>();
        //设置页码
        page.setCurrent(orderInfo.getCurrent());
        //设置行数
        page.setSize(orderInfo.getSize());
        //执行查询
        orderInfoService.page(page,new LambdaQueryWrapper<OrderInfo>()
                //创建时间
                .like(Objects.nonNull(orderInfo.getCreateTime()),OrderInfo::getCreateTime,formattedDate)
                //订单号
                .eq(Objects.nonNull(orderInfo.getOrderNo()),OrderInfo::getOrderNo,orderInfo.getOrderNo())
                //实付金额
                .eq(Objects.nonNull(orderInfo.getFund()),OrderInfo::getFund,orderInfo.getFund())
                //用户编号
                .eq(Objects.nonNull(orderInfo.getUserId()),OrderInfo::getUserId,orderInfo.getUserId())
                //订单备注
                .like(StringUtils.isNotBlank(orderInfo.getDescription()),OrderInfo::getDescription,orderInfo.getDescription())
                //订单状态
                .eq(Objects.nonNull(orderInfo.getOrderType()),OrderInfo::getOrderType,orderInfo.getOrderType())
                //会员id
                .eq(Objects.nonNull(orderInfo.getMemberId()),OrderInfo::getMemberId,orderInfo.getMemberId())
                //逻辑删除
                .eq(OrderInfo::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(OrderInfo::getCreateTime)
                //分页
                .last(" limit " + (orderInfo.getCurrent() - 1) * orderInfo.getSize() +"," + orderInfo.getSize())
        );
        page.setTotal(orderInfoService.count(new LambdaQueryWrapper<OrderInfo>()
                //创建时间
                .like(Objects.nonNull(orderInfo.getCreateTime()),OrderInfo::getCreateTime,formattedDate)
                //订单号
                .eq(Objects.nonNull(orderInfo.getOrderNo()),OrderInfo::getOrderNo,orderInfo.getOrderNo())
                //实付金额
                .eq(Objects.nonNull(orderInfo.getFund()),OrderInfo::getFund,orderInfo.getFund())
                //用户编号
                .eq(Objects.nonNull(orderInfo.getUserId()),OrderInfo::getUserId,orderInfo.getUserId())
                //订单备注
                .like(StringUtils.isNotBlank(orderInfo.getDescription()),OrderInfo::getDescription,orderInfo.getDescription())
                //订单状态
                .eq(Objects.nonNull(orderInfo.getOrderType()),OrderInfo::getOrderType,orderInfo.getOrderType())
                //会员id
                .eq(Objects.nonNull(orderInfo.getMemberId()),OrderInfo::getMemberId,orderInfo.getMemberId())
                //逻辑删除
                .eq(OrderInfo::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(OrderInfo::getCreateTime)
                //排序
                .orderByDesc(OrderInfo::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/OrderInfo/{id}")
    public AjaxResult deleteOrderInfo(@PathVariable(value = "id")Long id){
        //调用Service层删除
        orderInfoService.update(null,new UpdateWrapper<OrderInfo>().lambda()
                //主键id
                .eq(OrderInfo::getId,id)
                //设置逻辑删除为 真
                .set(OrderInfo::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }

    //增加
    @PostMapping("/insert/OrderInfo")
    public AjaxResult insertOrderInfo(@RequestBody OrderInfo orderInfo){
        //设置默认值
        orderInfo.setCreateBy(SecurityUtils.getUsername());
        orderInfo.setCreateTime(new Date());
        orderInfo.setUpdateBy(SecurityUtils.getUsername());
        orderInfo.setUpdateTime(new Date());
        orderInfo.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        orderInfoService.save(orderInfo);
        //返回结果
        return AjaxResult.success("新增成功");
    }

    //修改
    @PostMapping("/update/OrderInfo")
    public AjaxResult updateOrderInfo(@RequestBody OrderInfo orderInfo){
        //设置默认值
        orderInfo.setUpdateTime(new Date());
        orderInfo.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        orderInfoService.updateById(orderInfo);
        //返回结果
        return AjaxResult.success("修改成功");
    }


}
